package canglang.monitor.cases.dao;

import java.util.*;

import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.cases.model.CaseOneModel;
import canglang.monitor.cases.to.SearchOneTO;

@SuppressWarnings("unchecked")
public class CaseOneDAO extends BaseDAO {
    public Integer addCaseOne(CaseOneModel caseOneModel) {
        String sql = "addCaseOne";
        Map map = objectToMap(caseOneModel);
        return (Integer) getSqlMapClientTemplate().insert(sql, map);
    }

    public CaseOneModel getCaseOne(int caseOneId) {
        String sql = "getCaseOne";
        Map map = new HashMap();
        map.put("caseOneId", caseOneId);
        return (CaseOneModel) getSqlMapClientTemplate().queryForObject(sql, map);
    }

    public CaseOneModel saveCaseOne(CaseOneModel caseOneModel) {
        String sql = "saveCaseOne";
        Map map = objectToMap(caseOneModel);
        getSqlMapClientTemplate().update(sql, map);
        return this.getCaseOne(caseOneModel.getCaseOneId());
    }

    public void deleteCaseOne(int caseOneId) {
        String sql = "deleteCaseOne";
        Map map = new HashMap();
        map.put("caseOneId", caseOneId);
        getSqlMapClientTemplate().delete(sql, map);
    }

    public Integer searchCount(SearchOneTO searchTO) {
        Map map = new HashMap();
        map.put("deptId", searchTO.getDeptId());
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));
        return (Integer) getSqlMapClientTemplate().queryForObject("searchCaseOneCount", map);
    }

    public List searchList(PaginatedListHelper helper, SearchOneTO searchTO) {
        Map map = new HashMap();
        map.put("deptId", searchTO.getDeptId());
        map.put("dateFrom", searchTO.getDateFrom());
        if (searchTO.getDateTo() != null) {
            map.put("dateTo", DateUtil.add(searchTO.getDateTo(), Calendar.DATE, 1));
        }
        map.put("createId", StringUtil.convertEmpty2Null(searchTO.getCreateId()));
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchCaseOneList", map);
    }

    public List getCaseOneList(SearchOneTO searchTO) {
        Map map = new HashMap();
        //int deptId = searchTO.getDeptId();
        Date dateFrom = searchTO.getDateFrom();
        Date dateTo = searchTO.getDateTo();
        //map.put("deptId", deptId);
        map.put("dateFrom", dateFrom);
        if (dateTo != null) {
            map.put("dateTo", DateUtil.add(dateTo, Calendar.DATE, 1));
        }
        return getSqlMapClientTemplate().queryForList("getCaseOneList", map);
    }

    public Integer getCaseOneReportList(SearchOneTO searchTO) {
        Map map = new HashMap();
        Date dateFrom = searchTO.getDateFrom();
        Date dateTo = searchTO.getDateTo();
        map.put("dateFrom", dateFrom);
        if (dateTo != null) {
            map.put("dateTo", DateUtil.add(dateTo, Calendar.DATE, 1));
        }

        String rank = searchTO.getRank();
        String industry = searchTO.getIndustry();
        String hotInd = searchTO.getHotInd();
        String judgeAccusal = searchTO.getJudgeAccusal();
        int ageFrom=searchTO.getAgeFrom();
        int ageTo=searchTO.getAgeTo();

        int invAmountFrom=searchTO.getInvAmountFrom();
        int invAmountTo=searchTO.getInvAmountTo();


        if (rank != null) {
            map.put("rank", rank);
        }
        if (industry != null) {
            map.put("industry", industry);
        }
        if (hotInd != null) {
            map.put("hotInd", hotInd);
        }
        if (judgeAccusal != null) {
            map.put("judgeAccusal", judgeAccusal);
        }

        if (ageFrom >=0&&ageTo!=0) {
            map.put("ageFrom", ageFrom);
        }
        if (ageTo>0) {
            map.put("ageTo", ageTo);
        }
        if (invAmountFrom >=0&&invAmountTo!=0) {
            map.put("invAmountFrom",  invAmountFrom);
        }
        if (invAmountTo >0) {
            map.put("invAmountTo", invAmountTo);
        }
        return (Integer) getSqlMapClientTemplate().queryForObject("getCaseOneReportList", map);
    }


}
